﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonTagTime.PagerControl;
using CommonTagTime.Business;
using CommonTagTime.ModelObject;
using CommonTagTime;
public partial class admin_CategoryList : SitePage
{
    #region ViewState
    private string linkDetail = "CategoryDetail.aspx?id={0}&act=edit";
    public int count = 0;
    public int PageIndex
    {

        get
        {

            object o = ViewState["PageIndex"];

            if (o != null) return (int)o;

            return 0;

            //a default

        }

        set { ViewState["PageIndex"] = value; }

    }   
    public string SortField
    {

        get
        {
            object o = ViewState["SortField"];
            if (o != null) return (string)o;
            return string.Empty;
        }

        set { ViewState["SortField"] = value; }

    }
    public string SortExpression
    {

        get
        {
            object o = ViewState["SortExpression"];
            if (o != null) return (string)o;
            return string.Empty;
        }

        set { ViewState["SortExpression"] = value; }

    }
    #endregion

    #region Init,Loads
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            LoadData();
      //  Response.Write("Application:" + this.ApplicationName);
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        InitData();
    }
    #endregion

    #region Events
    protected void Pager2_PageIndexChanging(object obj, PageIndexChangeEventArgs e)
    {
        this.PageIndex = e.PageIndex;
        LoadData();
    }
    protected void Pager2_PageSizeChanging(object obj, PageSizeChangeEventArgs e)
    {
        this.PageIndex = 1;
        Pager2.PageSize = Pager1.PageSize = e.PageSize;
        LoadData();
    }
    protected void rptData_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Category cate = (Category)e.Item.DataItem;
            if (cate == null)
                return;
            hidCategoryID.Value += cate.CategoryID.ToString() + ";";
   
            //image button IsActive
            ImageButton imgButton = (ImageButton)e.Item.FindControl("imbActive");
            imgButton.CommandArgument = cate.CategoryID.ToString();
            if (cate.IsActive == true)
            {
                imgButton.ImageUrl = clsString.ACTIVE_IMAGE_URL;
                imgButton.ToolTip = Resources.Resource.TIP_DEACTIVE;
                imgButton.CommandName = "DeActive";
            }
            else
            {
                imgButton.ImageUrl = clsString.DEACTIVE_IMAGE_URL;
                imgButton.ToolTip = Resources.Resource.TIP_ACTIVE;
                imgButton.CommandName = "Active";
            }
            //image button Order
            imgButton = (ImageButton)e.Item.FindControl("imbOrderDown");
            if (e.Item.ItemIndex + 1 == count)
                imgButton.Visible = false;
            else
            {
                imgButton.ToolTip =Resources.Resource.TIP_MOVEDOWN;
                imgButton.CommandArgument = cate.CategoryID.ToString();
                imgButton.ImageUrl = clsString.ORDERDOWN_IMAGE_URL;
            }
            imgButton = (ImageButton)e.Item.FindControl("imbOrderUp");
            if (e.Item.ItemIndex == 0)
                imgButton.Visible = false;
            else
            {
                imgButton.CommandArgument = cate.CategoryID.ToString();
                imgButton.ToolTip = Resources.Resource.TIP_MOVEUP;
                imgButton.ImageUrl = clsString.ORDERUP_IMAGE_URL; ;
                imgButton.Visible = true;
            }

            //image button Edit
           // imgButton = (ImageButton)e.Item.FindControl("imbEdit");
           // imgButton.ToolTip = Resources.Resource.TIP_EDIT;
           // imgButton.CommandArgument = cate.CategoryID.ToString();
            //imgButton.ImageUrl = clsString.EDIT_IMAGE_URL;

            imgButton = (ImageButton)e.Item.FindControl("imbDelete");
            imgButton.ToolTip = Resources.Resource.TIP_DELETE;
            imgButton.Attributes.Add("onclick","return confirm('" + Resources.Resource.MSG_CONFIRM_DELETE  + "');");
            imgButton.CommandArgument = cate.CategoryID.ToString();
            imgButton.ImageUrl = clsString.DELETE_IMAGE_URL;
            System.Web.UI.HtmlControls.HtmlTableRow row = (System.Web.UI.HtmlControls.HtmlTableRow)e.Item.FindControl("trRow");
            if(e.Item.ItemIndex%2==0)
                row.Attributes.Add("class", "alt-row");
          
        }

    }
    protected void rptData_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

        if (e.CommandName == "SortOrder")
        {
            SortList(e.CommandArgument.ToString());

        }
        else
        {
            int id = clsConvert.Int(e.CommandArgument.ToString());
            switch (e.CommandName)
            {

                case "OrderUp":
                    {
                        OrderUp(id);
                        break;
                    }
                case "OrderDown":
                    {
                        OrderDown(id);
                        break;
                    }
                case "Active":
                    {
                        Active(id);
                        break;
                    }
                case "DeActive":
                    {
                        DeActive(id);
                        break;
                    }

                case "Delete":
                    {
                        Delete(id);
                        break;
                    }
            }
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        this.PageIndex = 1;
        LoadData();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {

        CategoryCollection lstCate = new CategoryCollection();
        string[] arr =hidCategoryIDSeelct.Value.Split(';');
        for (int i = 0; i < arr.Length; i++)
        {
            if (arr[i].ToString() != string.Empty)
            {
                Category objCate = new Category();
                objCate.CategoryID = clsConvert.Int(arr[i].ToString());
                lstCate.Add(objCate);
            }
        }
        busCategory objBus = new busCategory();
        objBus.DeleteMulti(lstCate);        
        LoadData();

    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("CategoryDetail.aspx");
    }

    #endregion

    #region Methods
    private void SortList(string sortField)
    {
        if (this.SortField == sortField)
        {
            if (this.SortExpression == "ASC")
                this.SortExpression = "DESC";
            else this.SortExpression = "ASC";
        }
        else
        {
            this.SortField = sortField;
            this.SortExpression = "ASC";
        }
        LoadData();
    }
    private void OrderDown(int id)
    {
        busCategory _busCategory = new busCategory();
        if (id >0 && _busCategory.OrderDown(id))
                LoadData();        
    }
    private void OrderUp(int id)
    {

        busCategory _busCategory = new busCategory();
        if (id > 0 && _busCategory.OrderUp(id))
               LoadData();        
    }
    private void Active(int id)
    {
        busCategory _busCategory = new busCategory();
        if (id > 0 && _busCategory.Active(id))
              LoadData();        
    }
    private void DeActive(int id)
    {
        busCategory _busCategory = new busCategory();
        if (id > 0 && _busCategory.DeActive(id))
             LoadData();        
    }
    private void Delete(int id)
    {
        busCategory _busCategory = new busCategory();
        if (id > 0 && _busCategory.Delete(id))
           LoadData();        
    }
    private void LoadData()
    {
        hidCategoryID.Value = string.Empty;
        //get Condition
        Category objCate = new Category();       
        objCate.Name = txtCategoryName.Text;
        objCate.CurrentPage = this.PageIndex;
        objCate.OrderBy = this.SortField;
        objCate.OrderDirection = this.SortExpression;
        objCate.PageSize = Pager1.PageSize;
        objCate.Condition = GetCondition(objCate);
        busCategory _busCategory = new busCategory();
        int total = 0;       
        CategoryCollection lstResult = _busCategory.List(objCate);
        total = objCate.TotalRecords;
        if (lstResult != null && lstResult.Count > 0)
             count = lstResult.Count;        
        rptData.DataSource = lstResult;
        rptData.DataBind();               
        Pager1.SetPaging(this.PageIndex, count, total);
        Pager2.SetPaging(this.PageIndex, count, total);
    }
    private void InitData()
    {
        this.PageIndex = 1;
        this.Pager2.PageSize = this.Pager1.PageSize = clsConfigData.DEFAULT_ADMIN_PAGE_SIZE;
        this.SortField = busCategory.SORTORDER;
        this.SortExpression = "ASC";
    }
    private string GetCondition(Category objCate)
    {
        string result = "1=1";       
        if (objCate.Name != string.Empty)
            result += " and c.Name like N'%" + objCate.Name + "%'";       
        return result;
    }
    #endregion   
}